package com.aiden.cashbook.base

import android.app.Activity
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentActivity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.trello.rxlifecycle2.components.support.RxFragment
/**
 * 懒加载fragment
 * @author aiden
 * @date 2018/9/4 15:52
 */
abstract class BaseFragment: RxFragment() {

    protected lateinit var mInflater: LayoutInflater
    protected var mContainer: ViewGroup? = null
    private var mIsVisibleToUser = false
    private var mIsLoadedData = false
    private var mIsViewCreated = false
    private var mIsHidden = true


    abstract fun initView(): View

    abstract fun initData()

    abstract fun setListener()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mInflater = inflater
        mContainer = container
        return initView()
    }

    /**
     * ViewPager场景下回调，在onActivityCreated回调之前执行
     */
    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        this.mIsVisibleToUser = isVisibleToUser
        tryToLoad()
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        this.mIsViewCreated = true
        setListener()
        tryToLoad()
    }

    /**
     * FragmentManager管理fragment场景下回调，在onActivityCreated回调后执行
     */
    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        this.mIsHidden = hidden
        if (!mIsHidden) {
            tryToLoad1()
        }
    }

    override fun onResume() {
        super.onResume()
        if (!mIsLoadedData) {
            tryToLoad1()
        }
    }

    /**
     * ViewPager场景下，判断父fragment是否可见
     */
    private fun isParentVisible(): Boolean {
        val fragment = parentFragment
        return fragment == null || (fragment is BaseFragment && fragment.mIsVisibleToUser)
    }

    /**
     * ViewPager场景下，若父fragment可见，则尝试让可见的子fragment加载数据
     */
    private fun dispatchParentVisibleState() {
        val fragments: List<Fragment> = fragmentManager!!.fragments
        if (fragments.isEmpty()) {
            return
        }
        for (child: Fragment in fragments) {
            if (child is BaseFragment && child.mIsVisibleToUser) {
                child.tryToLoad()
            }
        }
    }

    /**
     * ViewPager场景下加载数据
     */
    private fun tryToLoad() {
        if (mIsViewCreated && mIsVisibleToUser && isParentVisible() && !mIsLoadedData) {
            initData()
            mIsLoadedData = true
            dispatchParentVisibleState()
        }
    }

    /**
     * FragmentManager管理fragment情况下，判断父fragment是否隐藏
     */
    private fun isParentHidden(): Boolean {
        val fragment = parentFragment ?: return false
        if (fragment is BaseFragment && !fragment.isParentHidden()) {
            return false
        }
        return true
    }

    /**
     * FragmentManager管理Fragment情况下，当fragment显示时，对显示的子fragment尝试加载数据
     */
    private fun dispatchParentHiddenState() {
        val fragments: List<Fragment> = fragmentManager!!.fragments
        if (fragments.isEmpty()) {
            return
        }
        for (child in fragments) {
            if (child is BaseFragment && !child.mIsHidden) {
                child.tryToLoad1()
            }
        }
    }

    /**
     * FragmentManager管理fragment情况下，加载数据
     */
    private fun tryToLoad1() {
        if (mIsViewCreated && !isParentHidden() && !mIsLoadedData) {
            initData()
            mIsLoadedData = true
            dispatchParentHiddenState()
        }
    }

}